﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using DbAdapter.sqldbAdapter;

namespace Water.Model
{
    /// <summary>
    /// 下拉列表
    /// </summary>
    public class DropdownList
    {
        public int SID { get; set; }
        public string SELParent { get; set; }
        public string SELID { get; set; }        
        public string SELVALUE { get; set; }
        public string Memo { get; set; }

        /// <summary>
        /// 获取所有下拉菜单
        /// </summary>
        /// <returns></returns>
        public IList<DropdownList> GetAllDropDownLists()
        {
            string sql = "select * from tbt_downlist order by SELID";
            sqldb db = new sqldb();
            DataSet ds = db.ExecuteDataset(sqlconn.GetConnStr(), CommandType.Text, sql);
            IList<DropdownList> mrlist = null;
            if (ds != null && ds.Tables[0] != null && ds.Tables[0].Rows.Count > 0)
            {
                mrlist = new List<DropdownList>();
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    DropdownList objmenu = new DropdownList();
                    objmenu.SID = Convert.ToInt32(dr["SID"]);
                    objmenu.SELParent = dr["SELParent"].ToString();
                    objmenu.SELID = dr["SELID"].ToString();
                    objmenu.SELVALUE = dr["SELVALUE"].ToString();
                    objmenu.Memo = dr["Memo"].ToString();
                    mrlist.Add(objmenu);
                }
            }
            return mrlist;
        }
        /// <summary>
        /// get downlist by selparent
        /// </summary>
        /// <param name="selp"></param>
        /// <returns></returns>
        public IList<DropdownList> GetDropdownList(string selp)
        {
            string sql = "select * from tbt_downlist where SELParent = '" + selp + "' order by SELID";
            sqldb db = new sqldb();
            DataSet ds = db.ExecuteDataset(sqlconn.GetConnStr(), CommandType.Text, sql);
            IList<DropdownList> mrlist = null;
            if (ds != null && ds.Tables[0] != null && ds.Tables[0].Rows.Count > 0)
            {
                mrlist = new List<DropdownList>();
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    DropdownList objmenu = new DropdownList();
                    objmenu.SID = Convert.ToInt32(dr["SID"]);
                    objmenu.SELParent = dr["SELParent"].ToString();
                    objmenu.SELID = dr["SELID"].ToString();
                    objmenu.SELVALUE = dr["SELVALUE"].ToString();
                    objmenu.Memo = dr["Memo"].ToString();
                    mrlist.Add(objmenu);
                }
            }
            return mrlist;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="ddl"></param>
        /// <returns></returns>
        public int AddDropDownList(DropdownList ddl)
        {
            string sql = "Insert Into Tbt_DownList(SELParent,SELID,SELVALUE,Memo) Values('" + ddl.SELParent + "','"
                + ddl.SELID + "','" + ddl.SELVALUE + "','" + ddl.Memo + "')";
            sqldb db = new sqldb();
            return db.ExecuteNonQuery(sqlconn.GetConnStr(), CommandType.Text, sql);
        }

        /// <summary>
        /// 删除下拉列表项
        /// </summary>
        /// <param name="sid"></param>
        /// <returns></returns>
        public int DelDropDownList(int sid)
        {
            string sql = "Delete from Tbt_DownList where SID = " + sid.ToString();
            sqldb db = new sqldb();
            return db.ExecuteNonQuery(sqlconn.GetConnStr(), CommandType.Text, sql);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="ddl"></param>
        /// <returns></returns>
        public int UpdateDropDownList(DropdownList ddl)
        {
            string sql = "Update Tbt_DownList set SELParent = '" + ddl.SELParent + "',"
                       + "SELID = '" + ddl.SELID + "',SELVALUE = '" + ddl.SELVALUE + "',Memo = '" + ddl.Memo 
                       + "' where SID = " + ddl.SID.ToString();
            sqldb db = new sqldb();
            return db.ExecuteNonQuery(sqlconn.GetConnStr(), CommandType.Text, sql);
        }
    }
}
